// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { easing, tween, view } from "cc";
import { GComponent, GRoot, RelationType, Transition } from "fairygui-cc";
import { FuiCtrl } from "../fwk/puremvc/BaseCtrl";
import { Ctor, RegistPMVC } from "../fwk/puremvc/RegistFunc";

export function RFui(pkgName: string, comName: string) {
    return function (viewCtor: Ctor<FuiCom>) {
        FuiCtrl.registFui(viewCtor, pkgName, comName);
    }
}
export class FuiCom extends GComponent {
    public pkg: string = "";
    public com: string = "";
    static backShow(view: GComponent) {
        view.scaleX = view.scaleY = 0;
        view.pivotX = view.pivotY = 0.5;
        tween(view).to(0.5, { scaleY: 1, scaleX: 1 }, { easing: easing.backOut }).start();
    }
    get isPkgLoaded() {
        return FuiCtrl.isLoadPkg(this.pkg);
    }
    static clear() {
        if (this.initGroot)
            GRoot.inst.visible = false;
        this.initGroot = false;
    }
    setMiddle() {
        let screenSize = view.getVisibleSize();
        this.setPosition(screenSize.width / 2, screenSize.height / 2);
    }
    setWH() {
        let screenSize = view.getVisibleSize();
        this.setSize(screenSize.width, screenSize.height);
    }
    private static initGroot: boolean = false;
    protected isShow: boolean = false;
    public get IsShow() { return this.isShow; }
    onClear(): void {
        RegistPMVC.unbindView(Object.getPrototypeOf(this).constructor);
        this.dispose();
    }
    onCreate() {
        RegistPMVC.bindView(Object.getPrototypeOf(this).constructor, this);
        this.addRelation(GRoot.inst, RelationType.Width);
        this.addRelation(GRoot.inst, RelationType.Height);
        GRoot.inst.addChild(this);
    }
    static async awaitTrans(trans: Transition) {
        return new Promise<void>(resolve => {
            trans.play(() => {
                resolve();
            })
        });
    }
}
